/***************************************************************************
 *   oboe.controllers.soundController.ManagedSound                         *
 *   Copyright (C) 2011 by Felipe Manga                                    *
 *   lufeboma@gmail.com                                                    *
 *                                                                         *
 *   Permission is hereby granted, free of charge, to any person obtaining *
 *   a copy of this software and associated documentation files (the       *
 *   "Software"), to deal in the Software without restriction, including   *
 *   without limitation the rights to use, copy, modify, merge, publish,   *
 *   distribute, sublicense, and/or sell copies of the Software, and to    *
 *   permit persons to whom the Software is furnished to do so, subject to *
 *   the following conditions:                                             *
 *                                                                         *
 *   The above copyright notice and this permission notice shall be        *
 *   included in all copies or substantial portions of the Software.       *
 *                                                                         *
 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       *
 *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    *
 *   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
 *   IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR     *
 *   OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
 *   ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
 *   OTHER DEALINGS IN THE SOFTWARE.                                       *
 ***************************************************************************/

package oboe.controllers.soundController
{
	import flash.events.Event;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.SoundTransform;
	
	import oboe.core.Oboe;
	
	public class ManagedSound
	{
		private var sound:Sound;
		private var channels:Vector.< ManagedChannel >;
		private var onEnd:Function;
		private var name:String;
		private var soundClass:String;
		
		public function ManagedSound( sound:Sound, onEnd:Function, name:String, soundClass:String )
		{
			this.sound = sound;
			this.onEnd = onEnd;
			this.name = name;
			this.soundClass = soundClass;
			this.channels = new Vector.<ManagedChannel>();
		}
		
		public function getChannelCount():int
		{
			return channels.length;
		}
		
		public function getSoundClass():String
		{
			return this.soundClass;
		}
		
		public function removeChannel( channel:ManagedChannel ):void
		{
			var i:int = channels.indexOf( channel );
			if( i == -1 ) return;
			channels[i] = channels[ channels.length-1 ];
			channels.pop();
		}
		
		public function play( pos:Number, loops:int, transform:SoundTransform=null ):void
		{
			if( Oboe.flags["NO_SOUND"] || !this.sound ) return;
			this.channels.push( new ManagedChannel( this, pos, loops, transform, this.onEnd ) );
		}
		
		public function getName():String
		{
			return this.name;
		}
		
		public function getSound():Sound
		{
			return this.sound;
		}
		
		public function stop():void
		{
			if( !this.channels.length ) return;
			var currentChannel:ManagedChannel = this.channels.pop();
			currentChannel.stop( true );
		}
		
		public function stopAll():void
		{
			for each( var channel:ManagedChannel in this.channels )
			{
				channel.stop( true );
			}
			
			this.channels.length = 0;
		}
		
		public function pause():void
		{
			for each( var channel:ManagedChannel in this.channels )
			{
				channel.stop( false );
			}
		}
		
		public function setMute( muted:Boolean ):void
		{
			for each( var channel:ManagedChannel in this.channels )
			{
				channel.setMute( muted );
			}
		}
		
		public function resumeAfter( timeElapsed:Number ):void
		{
			for( var i:int=0; i<this.channels.length; ++i )
			{
				this.channels[i].resumeAfter( timeElapsed );
			}
		}
	}
	
}